﻿/*
 * đăng ký bán địa ốc
 * undone
 * btn lập phiếu thu
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using BLL;
using DTO;
using DevComponents.DotNetBar;

namespace GUI.View
{
    public partial class DangKyBanDiaOc : UserControl
    {
        #region khai báo biến

        //khao bao cac lop
        private BaoBLL baoBLL;
        private MauSacBLL mausacBLL;
        private ViTriBLL vitriBLL;
        private LoaiBangBLL loaibangBLL;
        private KichCoBangBLL kichcobangBLL;
        private ToBuomBLL tobuomBLL;
        private SoLuongToBuomBLL soluontobuomBLL;
        private NoiDungQCBLL noidungQCBLL;
        private QC_BangBLL QCbangBLL;
        private QCBaoBLL QCbaoBLL;
        private ToBuomBLL QCtobuomBLL;
        private PhieuDangKyBLL phieudangkyBLL;
        private ThamSoBLL thamsoBLL;
        private DiaOcBLL diaocBLL;
        private ChiTietBaoBLL chitietbaoBLL;
        private ChiTietBangBLL chitietbangBLL;
        private ChiTietToBuomBLL chitiettobuomBLL;

        //lay thong tin khach hang dia oc
        public static int _makh;
        public static string _email;
        public static string _sodt;
        public static string _diachi;
        public static string _hoten;
        public static int _madiaoc;
        public static string _loaidiaoc;
        public static string _diachidiaoc;
        public static string _huong;
        public static string _vitri;
        public static string _mota;
        public static string _giaban;
        public static float _dientichdat;
        public static float _dientichxaydung;
        public static int _maphieudangky;

        //tien dich vu
        private float _tienbao = 0;
        private float _tienbang = 0;
        private float _tientobuom = 0;

        //lay ma chi tiet dich vu
        int _machitietbao = 0;
        int _machitietbang = 0;
        int _machitiettobuom = 0;

        //lay gia tri thoi gian
        private string _tungay = "";
        private string _denngay = "";
        private string _ngayhen = "";
        private string _ngaybatdauphathanh = "";

        //kiem tra dia oc da dang ky da thanh toan chua
        public static bool kiemtratinhtrangthanhtoan = true;

        private bool kiemtradangky;

        public static int _kiemtrahople = 0;
        /*
         *  0 hợp lệ
         *  1 đã bán
         *  2 đã 
         */ 

        #endregion
        private AppCommands _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
     protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                //MessageBox.Show("in");
                btnCancel.Command = newValue.LapPhieuDangkyDiaOcCommands.Cancel;
            }
            else
            {
                btnCancel.Command = null;
            }
        }


        public DangKyBanDiaOc()
        {
            InitializeComponent();
            baoBLL = new BaoBLL();
            chitietbaoBLL = new ChiTietBaoBLL();
            chitietbangBLL = new ChiTietBangBLL();
            chitiettobuomBLL = new ChiTietToBuomBLL();

            mausacBLL = new MauSacBLL();
            vitriBLL = new ViTriBLL();
            loaibangBLL = new LoaiBangBLL();
            kichcobangBLL = new KichCoBangBLL();
            tobuomBLL = new ToBuomBLL();
            soluontobuomBLL = new SoLuongToBuomBLL();
            noidungQCBLL = new NoiDungQCBLL();
            QCbangBLL = new QC_BangBLL();
            QCbaoBLL = new QCBaoBLL();
            QCtobuomBLL = new ToBuomBLL();
            phieudangkyBLL = new PhieuDangKyBLL();
            thamsoBLL = new ThamSoBLL();
            diaocBLL = new DiaOcBLL();
            kiemtradangky = true;


            
        }

        public void loadinfo()
        {
            if (_kiemtrahople == 1)
            {
                ToastNotification.Show(this, "Địa Ốc Đã Được Bán", eToastPosition.MiddleCenter);
                return;
            }
            else if (_kiemtrahople == 2)
            {
                ToastNotification.Show(this, "Địa Ốc Đã Đăng Ký", eToastPosition.MiddleCenter);
                return;
            }

            if (kiemtratinhtrangthanhtoan)
            {
                txtGiaTien.Text = Tiendangkydiaoc().ToString();
                //string _queryThoihandangky = thamsoBLL.GetSoNgayDangKy();
                int thoihandangky = Int32.Parse(thamsoBLL.GetSoNgayDangKy());
                dateTime_TuNgay.Value = DateTime.Now;
                dateTime_DenNgay.Value = dateTime_TuNgay.Value.AddDays(thoihandangky);
                dateTime_NgayHen.Value = dateTime_TuNgay.Value;
                dateTime_NgayPhatHanh.Value = DateTime.Now;

                //load thong tin khach hang
                txtHoTen.Text = _hoten;
                cbbMaKH.Text = _makh.ToString();
                txtSoDienThoai.Text = _sodt;
                txtEmail.Text = _email;
                txtDiaChi.Text = _diachi;

                //load thong tin dia oc
                txtMaDiaOc.Text = _madiaoc.ToString();
                txtLoaiDiaOc.Text = _loaidiaoc;
                txtDiaChiDiaOc.Text = _diachidiaoc;
                txtDienTichDat.Text = _dientichdat.ToString();
                txtDienTichXayDung.Text = _dientichxaydung.ToString();
                txtHuong.Text = _huong;
                txtViTriDiaOc.Text = _vitri;
                txtGiaBanDiaOc.Text = _giaban;
                txtMoTa.Text = _mota;
                txtMaPhieuDangKy.Text = MaPhieuDangKY().ToString();
                loadBao();
                loadBang();
                loadToBuom();
                txtTongTien.Text = txtGiaTien.Text;
            }
            else
            {
                txtGiaTien.Text = Tiendangkydiaoc().ToString();

                //load thoi gian
                int thoihandangky = Int32.Parse(thamsoBLL.GetSoNgayDangKy());
                dateTime_TuNgay.Value = DateTime.Now;
                dateTime_DenNgay.Value = dateTime_TuNgay.Value.AddDays(thoihandangky);
                dateTime_NgayHen.Value = dateTime_TuNgay.Value;
                dateTime_NgayPhatHanh.Value = DateTime.Now;

                //load thong tin khach hang
                txtHoTen.Text = _hoten;
                cbbMaKH.Text = _makh.ToString();
                txtSoDienThoai.Text = _sodt;
                txtEmail.Text = _email;
                txtDiaChi.Text = _diachi;

                //load thong tin dia oc
                txtMaDiaOc.Text = _madiaoc.ToString();
                txtLoaiDiaOc.Text = _loaidiaoc;
                txtDiaChiDiaOc.Text = _diachidiaoc;
                txtDienTichDat.Text = _dientichdat.ToString();
                txtDienTichXayDung.Text = _dientichxaydung.ToString();
                txtHuong.Text = _huong;
                txtViTriDiaOc.Text = _vitri;
                txtGiaBanDiaOc.Text = _giaban;
                txtMoTa.Text = _mota;

                //
                txtMaPhieuDangKy.Text = MaPhieuDangKY().ToString();
                loadBao();
                loadBang();
                loadToBuom();
                
            }

        }
        private void DangKyBanDiaOc_Load(object sender, EventArgs e)
        {
             loadinfo();
        }

        #region Load thong tin

        //load thong tin bao
        public void loadBao()
        {
            cbbTenBao.DataSource = baoBLL.GetAll().Tables[0];
            cbbTenBao.DisplayMember = "TenBao";
            cbbTenBao.ValueMember = "MaBao";
            cbbMauSac.DataSource = mausacBLL.GetAll().Tables[0];
            cbbMauSac.DisplayMember = "TenMauSac";
            cbbMauSac.ValueMember = "MaMauSac";
            cbbViTri.DataSource = vitriBLL.GetAll().Tables[0];
            cbbViTri.DisplayMember = "TenViTri";
            cbbViTri.ValueMember = "MaViTri";

            txtGiaTienBao.Text = TinhTienBao();
        }

        //load thong tin bang
        public void loadBang()
        {
            cbbLoaiBang.DataSource = loaibangBLL.GetAll().Tables[0];
            cbbLoaiBang.DisplayMember = "TenLoaiBang";
            cbbLoaiBang.ValueMember = "MaLoaiBang";
            cbbKichCo.DataSource = kichcobangBLL.GetAll().Tables[0];
            cbbKichCo.DisplayMember = "KichCo";
            cbbKichCo.ValueMember = "MaKichCo";
            txtGiaTienBang.Text = TinhTienBang();
        }

        public void loadToBuom()
        {
            cbbSoLuongToBuom.DataSource = soluontobuomBLL.GetAll().Tables[0];
            cbbSoLuongToBuom.DisplayMember = "SoLuong";
            cbbSoLuongToBuom.ValueMember = "MaSoLuong";
            txtGiaTienToBuom.Text = TinhTienToBuom();
        }

        #endregion

        #region Hàm hỗ trợ
        public float Tiendangkydiaoc()
        {
            float giatien = float.Parse(thamsoBLL.GetGiaTienDangKy());
            return giatien;
        }
        public int MaPhieuDangKY()
        {
            int maphieuDangKy_max = phieudangkyBLL.GetMaMax();
            _maphieudangky = maphieuDangKy_max + 1;
            return _maphieudangky;
        }
        #endregion

        #region các hàm tính tiền
        private string TinhTienBao()
        {
            try
            {
                ChiTietBaoDTO ct = new ChiTietBaoDTO(Convert.ToInt32(cbbMauSac.SelectedValue.ToString()),
                                                       Convert.ToInt32(cbbViTri.SelectedValue.ToString()),
                                                       Convert.ToInt32(cbbTenBao.SelectedValue.ToString()));
                string giatien = chitietbaoBLL.GetGiaTien(ct);

                return giatien;
            }
            catch (Exception)
            {
                return "0";
            }
        }

        private string TinhTienBang()
        {
            try
            {
                ChiTietBangDTO chitietbang = new ChiTietBangDTO(Convert.ToInt32(cbbLoaiBang.SelectedValue.ToString()), Convert.ToInt32(cbbKichCo.SelectedValue.ToString()));

                string giatien = chitietbangBLL.GetGiaTien(chitietbang);
                return giatien;
            }
            catch (Exception)
            {
                return "0";
            }
        }

        private string TinhTienToBuom()
        {
            try
            {
                ChiTietToBuomDTO cttobuom = new ChiTietToBuomDTO(Convert.ToInt32(cbbSoLuongToBuom.SelectedValue.ToString()));
                string giatien = chitiettobuomBLL.GetGiaTien(cttobuom);
                return giatien;
            }
            catch (Exception)
            {
                return "0";
            }
        }
        #endregion

        #region EVENT

        #region cbbViTri
        private void cbbViTri_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienBao.Text = TinhTienBao();
                _tienbao = float.Parse(txtGiaTienBao.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
                txtGiaTienBao.Enabled = false;
            }
        }

        private void cbbViTri_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienBao.Text = TinhTienBao();
            _tienbao = float.Parse(txtGiaTienBao.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
        }
        #endregion

        #region CbbmauSac
        private void cbbMauSac_MouseMove(object sender, MouseEventArgs e)
        {

            txtGiaTienBao.Text = TinhTienBao();
            _tienbao = float.Parse(txtGiaTienBao.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            txtGiaTienBao.Enabled = false;
        }
        private void cbbMauSac_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienBao.Text = TinhTienBao();
                _tienbao = float.Parse(txtGiaTienBao.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
                txtGiaTienBao.Enabled = false;
            }
        }
        #endregion

        #region cbbthuPhatHanh
        private void cbbThuPhatHanh_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienBao.Text = TinhTienBao();
            _tienbao = float.Parse(txtGiaTienBao.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            txtGiaTienBao.Enabled = false;
        }
        private void cbbThuPhatHanh_MouseClick(object sender, MouseEventArgs e)
        {
            txtGiaTienBao.Text = TinhTienBao();
            _tienbao = float.Parse(txtGiaTienBao.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            txtGiaTienBao.Enabled = false;
        }
        #endregion

        #region cbbtenBao
        private void cbbTenBao_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienBao.Text = TinhTienBao();
            _tienbao = float.Parse(txtGiaTienBao.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            txtGiaTienBao.Enabled = false;
        }
        private void cbbTenBao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienBao.Text = TinhTienBao();
                _tienbao = float.Parse(txtGiaTienBao.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
                txtGiaTienBao.Enabled = false;
            }
        }
        #endregion

        #region cbbKichCo
        private void cbbKichCo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienBang.Text = TinhTienBang();
                _tienbang = float.Parse(txtGiaTienBang.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            }
        }
        private void cbbKichCo_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienBang.Text = TinhTienBang();
            _tienbang = float.Parse(txtGiaTienBang.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
        }
        #endregion

        #region cbbloaiBang
        private void cbbLoaiBang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienBang.Text = TinhTienBang();
                _tienbang = float.Parse(txtGiaTienBang.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            }
        }

        private void cbbLoaiBang_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienBang.Text = TinhTienBang();
            _tienbang = float.Parse(txtGiaTienBang.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
        }
        #endregion

        #region cbbsoluongToBuom
        private void cbbSoLuongToBuom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtGiaTienToBuom.Text = TinhTienToBuom();
                _tientobuom = float.Parse(txtGiaTienToBuom.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
                txtGiaTienToBuom.Enabled = false;
            }
        }

        private void cbbSoLuongToBuom_MouseMove(object sender, MouseEventArgs e)
        {
            txtGiaTienToBuom.Text = TinhTienToBuom();
            _tientobuom = float.Parse(txtGiaTienToBuom.Text);
            txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();
            txtGiaTienToBuom.Enabled = false;
        }
        #endregion

        private void cbToBuom_CheckedChanged(object sender, EventArgs e)
        {
            if (cbToBuom.Checked == true)
            {
                gpToBuom.Enabled = true;
                _tientobuom = float.Parse(txtGiaTienToBuom.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
            else
            {
                gpToBuom.Enabled = false;
                _tientobuom = 0;
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
        }

        private void cbBao_CheckedChanged(object sender, EventArgs e)
        {
            if (cbBao.Checked == true)
            {
                gpBao.Enabled = true;
                _tienbao = float.Parse(txtGiaTienBao.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
            else
            {
                gpBao.Enabled = false;
                _tienbao = 0;
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
        }

        private void cbBang_CheckedChanged(object sender, EventArgs e)
        {
            if (cbBang.Checked == true)
            {
                gpBang.Enabled = true;
                _tienbang = float.Parse(txtGiaTienBang.Text);
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
            else
            {
                gpBang.Enabled = false;
                _tienbang = 0;
                txtTongTien.Text = (_tienbao + _tienbang + _tientobuom + float.Parse(txtGiaTien.Text)).ToString();

            }
        }

        private void dateTime_TuNgay_ValueChanged(object sender, EventArgs e)
        {
            int thoihandangky = Int32.Parse(thamsoBLL.GetSoNgayDangKy());
            dateTime_DenNgay.Value = dateTime_TuNgay.Value.AddDays(thoihandangky);
            dateTime_NgayHen.Value = dateTime_TuNgay.Value;
        }

        #endregion

        #region Button event
        private void btnLapPhieuThu_Click(object sender, EventArgs e)
        {
            if (!kiemtradangky)
            {
                ToastNotification.Show(this, "Địa Ốc Chưa Đăng Ký", eToastPosition.MiddleCenter);
                return;
            }

            if (cbBang.Checked == true)
                LapPhieuThuTien._kiemtraBang = true;
            else
                LapPhieuThuTien._kiemtraBang = false;
            if (cbBao.Checked == true)
                LapPhieuThuTien._kiemtraBao = true;
            else
                LapPhieuThuTien._kiemtraBao = false;
            if (cbToBuom.Checked == true)
                LapPhieuThuTien._kiemtraTobuom = true;
            else
                LapPhieuThuTien._kiemtraTobuom = false;

            LapPhieuThuTien._tiendangky = float.Parse(txtGiaTien.Text);
            //bao
            LapPhieuThuTien._tenbao = cbbTenBao.Text;
            LapPhieuThuTien._mausac = cbbMauSac.Text;
            LapPhieuThuTien._giatienbao = txtGiaTienBao.Text;
            LapPhieuThuTien._thuphathanh = cbbThuPhatHanh.GetItemText(cbbThuPhatHanh.SelectedItem).Trim();
            LapPhieuThuTien._vitribao = cbbViTri.Text;
            //bang
            LapPhieuThuTien._loaibang = cbbLoaiBang.Text;
            LapPhieuThuTien._kichco = cbbKichCo.Text;
            LapPhieuThuTien._giatienbang = txtGiaTienBang.Text;
            //to buom
            LapPhieuThuTien._soluong = cbbSoLuongToBuom.Text;
            LapPhieuThuTien._giatientobuom = txtGiaTienToBuom.Text;
            LapPhieuThuTien._tongtien = txtTongTien.Text;
            //thoi gian
            LapPhieuThuTien._tungay = String.Format("{0}/{1}/{2}", dateTime_TuNgay.Value.Year, dateTime_TuNgay.Value.Month, dateTime_TuNgay.Value.Day);
            LapPhieuThuTien._denngay = String.Format("{0}/{1}/{2}", dateTime_DenNgay.Value.Year, dateTime_DenNgay.Value.Month, dateTime_DenNgay.Value.Day);
            LapPhieuThuTien._ngaybatdauphathanh = String.Format("{0}/{1}/{2}", dateTime_NgayPhatHanh.Value.Year, dateTime_NgayPhatHanh.Value.Month, dateTime_NgayPhatHanh.Value.Day);
            //
            LapPhieuThuTien._chon = 1;   //gia tri xac dinh lap phieu thu duoc chon tu from dang ky


            //btnLapPhieuThu.Command = _Commands.LapPhieuThuTienCommands.New;

            // TEST create new PhieuThu    
            using (Form f = new Form())
            {

                f.Controls.Add(new LapPhieuThuTien());
                f.SetBounds(0, 0, 1000, 660);
                f.FormBorderStyle = FormBorderStyle.FixedDialog;
                f.StartPosition = FormStartPosition.CenterScreen;
                //f.WindowState = FormWindowState.Maximized;
                f.ShowDialog();
            }
        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            if (txtNoiDungQC.Text.Trim().Equals(""))
            {
                ToastNotification.Show(this, "Bạn chưa nhập nội dung quảng cáo", eToastPosition.MiddleCenter);
                return;
            }
            if (cbBang.Checked == false && cbBao.Checked == false && cbToBuom.Checked == false)
            {
                ToastNotification.Show(this, "Bạn chưa chọn dịch vụ quảng cáo", eToastPosition.MiddleCenter);
                return;
            }
            if (cbBao.Checked == true && (txtGiaTienBao.Text.Equals("0.0000") || cbbThuPhatHanh.GetItemText(cbbThuPhatHanh.SelectedItem).Trim() == null || cbbTenBao.Text.Equals("")
                || cbbMauSac.Text.Equals("") || cbbViTri.Text.Equals("")))
            {
                ToastNotification.Show(this, "Bạn chưa chọn chi tiết dịch vụ quảng cáo Báo", eToastPosition.MiddleCenter);
                return;
            }
            if (cbBang.Checked == true && (txtGiaTienBang.Text.Equals("0.0000") || cbbLoaiBang.Text.Equals("") || cbbKichCo.Text.Equals("") || cbbKichCo.Text.Equals("0")))
            {
                ToastNotification.Show(this, "Bạn chưa chọn chi tiết dịch vụ quảng cáo Bảng", eToastPosition.MiddleCenter);
                return;
            }
            if (cbToBuom.Checked == true && (txtGiaTienToBuom.Text.Equals("0.0000") || cbbSoLuongToBuom.Text.Equals("") || cbbSoLuongToBuom.Text.Equals("0")))
            {
                ToastNotification.Show(this, "Bạn chưa chọn chi tiết dịch vụ quảng cáo Tờ Bướm", eToastPosition.MiddleCenter);
                return;
            }

            _tungay = String.Format("{0}/{1}/{2}", dateTime_TuNgay.Value.Year, dateTime_TuNgay.Value.Month, dateTime_TuNgay.Value.Day);
            _denngay = String.Format("{0}/{1}/{2}", dateTime_DenNgay.Value.Year, dateTime_DenNgay.Value.Month, dateTime_DenNgay.Value.Day);
            _ngayhen = String.Format("{0}/{1}/{2}", dateTime_NgayHen.Value.Year, dateTime_NgayHen.Value.Month, dateTime_NgayHen.Value.Day);
            _ngaybatdauphathanh = String.Format("{0}/{1}/{2}", dateTime_NgayPhatHanh.Value.Year, dateTime_NgayPhatHanh.Value.Month, dateTime_NgayPhatHanh.Value.Day);

            int _mand = 0;
            int _maqcbao = 1;
            int _maqcbang = 1;
            int _maqctobuom = 1;
            int _cohinhanh = 0;

            if (cbCoThemAnhHayKhong.Checked == true)
                _cohinhanh = 1;
            else
                _cohinhanh = 0;

            NoiDungQCDTO nd = new NoiDungQCDTO(txtNoiDungQC.Text.Trim());
            if ((_mand = noidungQCBLL.Add(nd)) > 0)
            {
                if (cbBao.Checked == true)
                {
                    ChiTietBaoDTO chitietbao = new ChiTietBaoDTO(int.Parse(cbbMauSac.SelectedValue.ToString()),
                        int.Parse(cbbViTri.SelectedValue.ToString()), int.Parse(cbbTenBao.SelectedValue.ToString()));

                    int machitietbao = chitietbaoBLL.GetID(chitietbao);

                    if ((_machitietbao = machitietbao) == 0)
                    {
                        MessageBox.Show("Lỗi Rồi");
                        return;
                    }
                    QCBaoDTO qcbao = new QCBaoDTO(dateTime_NgayPhatHanh.Value, 
                                                  _cohinhanh, _mand, 
                                                  _machitietbao, 
                                                  cbbThuPhatHanh.GetItemText(cbbThuPhatHanh.SelectedItem).Trim());
                    if ((_maqcbao = QCbaoBLL.Add(qcbao)) == -1)
                    {
                        ToastNotification.Show(this, "Lỗi Báo", eToastPosition.MiddleCenter);
                        return;
                    }
                }

                if (cbBang.Checked == true)
                {
                    ChiTietBangDTO chitietbang = new ChiTietBangDTO(int.Parse(cbbLoaiBang.SelectedValue.ToString()), 
                                                                    int.Parse(cbbKichCo.SelectedValue.ToString()));

                    int machitietbang = chitietbangBLL.GetID(chitietbang);
                    if ((_machitietbang = machitietbang) == 0)
                    {
                        MessageBox.Show("Lỗi Rồi");
                        return;
                    }
                    QC_BangDTO qcbang = new QC_BangDTO(_mand, _machitietbang);
                    if ((_maqcbang = QCbangBLL.Add(qcbang)) == -1)
                    {
                        ToastNotification.Show(this, "Lỗi Bảng", eToastPosition.MiddleCenter);
                        return;
                    }
                }
                if (cbToBuom.Checked == true)
                {
                    ChiTietToBuomDTO chitiettobuom = new ChiTietToBuomDTO(int.Parse(cbbSoLuongToBuom.SelectedValue.ToString()));
                    int machitiettobuom = chitiettobuomBLL.GetID(chitiettobuom);

                    _machitiettobuom = machitiettobuom;
                    ToBuomDTO qctobuom = new ToBuomDTO(_mand, _machitiettobuom);
                    if ((_maqctobuom = QCtobuomBLL.Add(qctobuom)) == -1)
                    {
                        ToastNotification.Show(this, "Lỗi Tờ Bướm", eToastPosition.MiddleCenter);
                        return;
                    }
                }
            }
            else
            {
                ToastNotification.Show(this, "Không thêm được nội dung", eToastPosition.MiddleCenter);
                return;
            }

            PhieuDangKyDTO dk = new PhieuDangKyDTO(Int32.Parse(txtMaDiaOc.Text.Trim()),
                                                    dateTime_TuNgay.Value,
                                                    dateTime_DenNgay.Value,
                                                    float.Parse(txtTongTien.Text.Trim()),
                                                    dateTime_NgayHen.Value,
                                                    _maqctobuom,
                                                    _maqcbao,
                                                    _maqcbang,
                                                    0);
            if (phieudangkyBLL.Add(dk) > 0)
            {
                DiaOcDTO diaoc = new DiaOcDTO(Convert.ToInt32(txtMaDiaOc.Text));

                if (diaocBLL.DangKy(diaoc))
                {
                    ToastNotification.Show(this, "Đăng ký thành công", eToastPosition.MiddleCenter);
                }
                else
                    ToastNotification.Show(this, "Lỗi Rồi", eToastPosition.MiddleCenter);

                gpBang.Enabled = false;
                gpBao.Enabled = false;
                gpToBuom.Enabled = false;
                gpDiaOc.Enabled = false;
                gpKhachHang.Enabled = false;
                btnDangKy.Enabled = false;
                lblThongBao.ForeColor = Color.Red;
                lblThongBao.Text = "Chưa Lập phiếu Thu";
                kiemtradangky = true;
            }
            else
            {
                ToastNotification.Show(this, "Failed", eToastPosition.MiddleCenter);
            }

        }
        #endregion
    }
}
